home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / os2 / adaptor.zip / ADAPT.ZIP / adaptor / src / xunits.c < prev    next >
C/C++ Source or Header  |  1993-07-08  |  11KB  |  282 lines

  1. /**************************************************************************
  2. *                                                                         *
  3. *  Author      : Dr. Thomas Brandes, GMD, I1.HR                           *
  4. *  Date        : Nov 92                                                   *
  5. *  Last Update : April 92                                                 *
  6. *                                                                         *
  7. *  Module      : xvars                                                    *
  8. *                                                                         *
  9. *  Function    : Athena Widgets for handling unit items of the            *
  10. *                whole program                                            *
  11. *                                                                         *
  12. *  Export :                                                               *
  13. *                                                                         *
  14. *                                                                         *
  15. **************************************************************************/
  16.  
  17. /****************************************************************************
  18. *                                                                           *
  19. *    list :  units                                                          *
  20. *                                                                           *
  21. ****************************************************************************/
  22.  
  23. #include "xglobal.h"  /* import units, UnitCount */
  24. #include "Definitions.h"  /* import units, UnitCount */
  25. #include "ShowDefs.h"     /* imports SemFile, ShowDeclarations () */
  26. #include <Idents.h>
  27.  
  28. extern char * malloc();    /* no include file specified */
  29.  
  30. Widget units_popup, units_menu;
  31.  
  32. /****************************************************************************
  33. *                                                                           *
  34. *   UnitButton: is called when button is released                           *
  35. *               pops units_popup up if it is button3                        *
  36. *                                                                           *
  37. ****************************************************************************/
  38.  
  39. void UnitButton (w, client_data, event)
  40. Widget w;
  41. XtPointer client_data;
  42. XEvent *event;
  43. {
  44.   if (event->xbutton.button == Button3)
  45.     {   XtVaSetValues (units_popup, XtNx, event->xbutton.x_root, 
  46.                                     XtNy, event->xbutton.y_root, NULL); 
  47.  
  48.         /***************************************
  49.         *  Pop Up the Commands for Units       *
  50.         ***************************************/
  51.  
  52.         XtPopup (units_popup, XtGrabNonexclusive);
  53.     }
  54. }
  55.  
  56. /****************************************************************************
  57. *                                                                           *
  58. *   For a given declaration tree the position in the source                 *
  59. *   will be hightlighted (called by Unit- and Var-select                    *
  60. *                                                                           *
  61. ****************************************************************************/
  62.  
  63. void show_selected_decl (t)
  64. tTree t;
  65. { int l, length;
  66.   char s[100];
  67.   GetString (t->DECL_NODE.Name, s);
  68.   length = strlen (s);
  69.   l = t->DECL_NODE.Pos;
  70.   XawTextSetSelection (mytext, l-1, l+length-1);
  71.   XawTextSetInsertionPoint (mytext, l-1);
  72. }
  73.  
  74. /****************************************************************************
  75. *                                                                           *
  76. *   SetSelectedUnitItems:  Called when selecting a unit item                *
  77. *                                                                           *
  78. ****************************************************************************/
  79.  
  80. void SetSelectedUnitItems (call_data)
  81. XawListReturnStruct *call_data;
  82.  
  83. /* returns local declaration table of current selected unit */
  84.  
  85. { tIdent  Ident;
  86.  
  87.   /* Searching the Entry for the unit */
  88.  
  89.   Ident = MakeIdent (call_data->string, strlen (call_data->string));
  90.   UnitDecl = GetDeclEntry (Ident, GetUnitEntries () );
  91.  
  92.   /* set global values for selected unit */
  93.  
  94.   if (UnitDecl == NoObject)
  95.     { printf ("Unit %s not found in Table \n", call_data->string);
  96.       UnitTree = NoTree;
  97.       UnitDeclarations = NoDefinitions; 
  98.     }
  99.    else 
  100.     { UnitTree = UnitDecl->Object.decl;
  101.       show_selected_decl (UnitTree);
  102.       if (UnitDecl->Kind == kProcObject)
  103.           UnitDeclarations = UnitDecl->ProcObject.Declarations;
  104.       else if (UnitDecl->Kind == kFuncObject)
  105.           UnitDeclarations = UnitDecl->FuncObject.Declarations;
  106.       else if (UnitDecl->Kind == kBlockObject)
  107.           UnitDeclarations = UnitDecl->BlockObject.Declarations;
  108.       else { printf ("Illegal Declaration for a unit\n");
  109.              UnitDeclarations = NoDefinitions;
  110.            }
  111.     }
  112. }
  113.  
  114. /****************************************************************************
  115. *                                                                           *
  116. *   UnitSelect: This function is called after selecting a unit item         *
  117. *                                                                           *
  118. ****************************************************************************/
  119.  
  120. void UnitSelect (w, client_data, call_data)
  121. Widget w;
  122. XtPointer  client_data;
  123. XawListReturnStruct *call_data;
  124. { printf ("Unit %s = item %d has been selected\n",
  125.            call_data->string, call_data->list_index);
  126.  
  127.   if (call_data->list_index >= UnitCount)
  128.       printf ("No function \n");
  129.    else
  130.       { SetSelectedUnitItems (call_data);
  131.         show_vars (UnitDeclarations);
  132.       }
  133.  
  134. } /* UnitSelect */
  135.  
  136. /* predefined list of unit items : must not be empty  */
  137.  
  138. static String unit_items [] = { "<list_of_units>" };
  139.  
  140. /****************************************************************************
  141. *                                                                           *
  142. *   UnitMenuSelect: Called after selecting a unit command                   *
  143. *                                                                           *
  144. ****************************************************************************/
  145.  
  146. void UnitMenuSelect (w, client_data, garbage)
  147. Widget w;
  148. XtPointer client_data;
  149. XtPointer garbage;  /* call_data */
  150. {   Widget menu, father;
  151.     char *name;
  152.     FILE *myfile;
  153.     int i = (int) client_data;
  154.  
  155.     /* find the current unit */
  156.  
  157.     if (UnitDecl != NoObject)
  158.       {  if (i==1)  /* Unparse Unit */
  159.             { BeginUnparse ();
  160.               Unparse (UnitTree);
  161.               CloseUnparse ();
  162.               xshowfile (w, unparse_file);
  163.             }
  164.          if (i==2)  /* Write Unit */
  165.             { myfile = fopen ("test.out","w");
  166.               WriteTree (myfile, UnitTree);
  167.               fclose (myfile);
  168.               xshowfile (w, "test.out");
  169.             }
  170.          if (i==3)  /* Show Declarations */
  171.             { SemFile = fopen ("test.sem","w");
  172.               ShowDeclarations (UnitDeclarations);
  173.               fclose (SemFile);
  174.               xshowfile (w, "test.sem");
  175.             }
  176.       }
  177.     XtPopdown (units_popup);
  178. }
  179.  
  180. /****************************************************************************
  181. *                                                                           *
  182. *    init_units : creates list widget (exported)                            *
  183. *                                                                           *
  184. ****************************************************************************/
  185.  
  186. void init_units (parent)
  187. Widget parent;
  188. { int n;
  189.   Arg args[10];
  190.   Widget entry;
  191.   int x, y;
  192.  
  193.   n = 0;
  194.   XtSetArg (args[n], XtNnumberStrings, 1);  n++;
  195.   XtSetArg (args[n], XtNlist, unit_items);  n++;
  196.   XtSetArg (args[n], XtNdefaultColumns, 1);  n++;
  197.   XtSetArg (args[n], XtNcolumnSpacing, 15);  n++;
  198.  
  199.   units = XtCreateManagedWidget(
  200.           "units",                         /* widget name */
  201.           listWidgetClass,                 /* widget class */
  202.           parent,                          /* parent widget*/
  203.           args, n);                        /* varargs list */
  204.  
  205.   UnitCount = 0;
  206.   UnitTree  = NoTree;
  207.   UnitDecl  = NoObject;
  208.   UnitDeclarations = NoDefinitions;
  209.  
  210.   XtAddCallback(units, XtNcallback, UnitSelect, 0);
  211.  
  212.   XtAddEventHandler (units, ButtonReleaseMask, FALSE, UnitButton, 0);
  213.  
  214.   units_popup =  XtCreatePopupShell ("UnitMenu", transientShellWidgetClass,
  215.                             units, NULL, 0);
  216.  
  217.   /***************************************
  218.   *  Create the Commands for units       *
  219.   ***************************************/
  220.  
  221.   units_menu =  XtCreateManagedWidget ("unitmenu", boxWidgetClass,
  222.                             units_popup, NULL, 0);
  223.  
  224.   entry = XtCreateManagedWidget ("Unparse Unit",
  225.                   commandWidgetClass, units_menu, NULL, 0);
  226.   XtAddCallback (entry, XtNcallback, UnitMenuSelect, (XtPointer) 1);
  227.   entry = XtCreateManagedWidget ("Write Unit",
  228.                   commandWidgetClass, units_menu, NULL, 0);
  229.   XtAddCallback (entry, XtNcallback, UnitMenuSelect, (XtPointer) 2);
  230.   entry = XtCreateManagedWidget ("Show Declarations",
  231.                   commandWidgetClass, units_menu, NULL, 0);
  232.   XtAddCallback (entry, XtNcallback, UnitMenuSelect, (XtPointer) 3);
  233.   entry = XtCreateManagedWidget ("Quit",
  234.                   commandWidgetClass, units_menu, NULL, 0);
  235.   XtAddCallback (entry, XtNcallback, UnitMenuSelect, (XtPointer) 4);
  236.  
  237. }
  238.  
  239. /****************************************************************************
  240. *                                                                           *
  241. *    show_units : updates list widget (exported)                            *
  242. *                                                                           *
  243. ****************************************************************************/
  244.  
  245. void show_units ()
  246. {  char Item[250];
  247.    tDefinitions Entries, Elem;
  248.    int i;
  249.    String H;
  250.  
  251.    /* set the unit names in the unit-widget */
  252.  
  253.    Entries = GetUnitEntries () ;  /* imported from Definitions.h */
  254.    UnitCount = 0;
  255.    while (Entries->Kind != kENTRY_EMPTY)
  256.       { Elem = Entries->ENTRY_LIST.Elem;
  257.         GetString (Elem->Object.ident, Item);
  258.         if (UnitCount == MaxUnitItems)
  259.            printf ("ERROR: maximal number of Unit Items reached\n");
  260.         UnitItems[UnitCount] =  malloc (strlen (Item) + 1);
  261.         strcpy (UnitItems[UnitCount],Item);
  262.         UnitCount += 1;
  263.         Entries = Entries->ENTRY_LIST.Next;
  264.       }
  265.  
  266.    /* reverse the list: 0 <-> N-1, 1 <-> N-2, ... */
  267.    
  268.    for (i=0;i<UnitCount/2;i++)
  269.      { H = UnitItems[i];
  270.        UnitItems[i] = UnitItems[UnitCount-1-i];
  271.        UnitItems[UnitCount-1-i] = H;
  272.      }
  273.  
  274.    XawListChange (units, UnitItems, UnitCount, -1, TRUE);
  275.  
  276.    /* no selected unit */
  277.    UnitTree = NoTree;
  278.    UnitDecl = NoObject;
  279.    UnitDeclarations = NoDefinitions;
  280.  
  281. }
  282.